'''
Description: 
Author: 爱酱丶
Date: 2022-07-22 17:46:03
LastEditTime: 2022-07-22 18:05:20
LastEditors: 爱酱丶
'''
import os
from peps_download_best import PepsDownloadBest
import concurrent.futures
import json
from utils import province2tiles


def downloader(options):
    PepsDownloadBest(collection=options['collection'],
                     auth=options['auth'],
                     tile=options['tile'],
                     start_date=options['start_date'],
                     search_json_file=options['search_json_file'],
                     end_date=options['end_date'],
                     write_dir=options['write_dir'],
                     best_number=1
                     )


def data_download(l1c_dir, tiles, start_date, end_data, max_workers):
    options_list = []
    for tile in tiles:
        search_json = l1c_dir + '/' + tile + '_' + start_date + '.json'
        options_list.append(dict(collection='S2ST',
                                 auth='./conf/peps.txt',
                                 tile=tile,
                                 start_date=start_date,
                                 search_json_file=search_json,
                                 end_date=end_data,
                                 write_dir=l1c_dir

                                 )
                            )

    with concurrent.futures.ProcessPoolExecutor(max_workers=max_workers) \
            as executor:
        for options in options_list:
            executor.submit(downloader, options)


# 按照省下载哨兵数据
def download_province_sentinel():
    start_date = '2021-09-01'
    end_data = '2021-10-01'
    province = '吉林省'
    tiles = province2tiles(province)[0:1]
    l1c_dir = '/data/Lixujia/test'
    if not os.path.exists(l1c_dir):
        os.makedirs(l1c_dir)
    data_download(l1c_dir, tiles, start_date, end_data, 1)


def data_download_jiabing(l1c_dir, tiles, start_date, end_data, max_workers):
    options_list = []
    for tile in tiles:
        search_json = l1c_dir + '/' + tile + '_' + start_date + '.json'
        options_list.append(dict(collection='S2ST',
                                 auth='./conf/peps.txt',
                                 tile=tile,
                                 start_date=start_date,
                                 search_json_file=search_json,
                                 end_date=end_data,
                                 write_dir=l1c_dir

                                 )
                            )

    with concurrent.futures.ProcessPoolExecutor(max_workers=max_workers) \
            as executor:
        for options in options_list:
            executor.submit(downloader, options)


# 按照省下载哨兵数据
def download_province_sentinel_jiabing():
    start_date = '2020-10-01'
    end_data = '2020-11-01'
    province = '河南省'
    # tiles = province2tiles(province)
    json_path= r"E:\RasterShapeToolBox\conf\ChinaMgrs.json"
    with open(json_path,'r',encoding='utf-8') as file:
        china_tiles = json.load(file)
    tiles=china_tiles[province]
    l1c_dir = 'E:\Image2'
    if not os.path.exists(l1c_dir):
        os.makedirs(l1c_dir)
    data_download_jiabing(l1c_dir, tiles, start_date, end_data, 10)



if __name__ == '__main__':
    # download_province_sentinel()
    download_province_sentinel_jiabing()
